$(document).ready(function() {
    $('#username').bind('input oninput', function(event) {
        //input 即实时监控键盘输入包括粘贴
        var pwd = $(this).val();
        //获取this，即username的val()值
        var rpwd = $("#password").val();
        if(rpwd!=""){
            if(pwd != rpwd){
                document.getElementById("warning1").style.display="block";
                document.getElementById("warning").style.display="none";
                document.getElementById("warning2").style.display="none";
                document.getElementById("warning3").style.display="none";
                $("#submit").attr("disabled",true);
            }
            else{                          
                document.getElementById("warning").style.display="block";
                document.getElementById("warning2").style.display="none";
                document.getElementById("warning1").style.display="none";
                document.getElementById("warning3").style.display="none";
                $("#submit").attr("disabled",false);
            }
        }else {
            document.getElementById("warning2").style.display="block";
            document.getElementById("warning").style.display="none";
            document.getElementById("warning1").style.display="none";
            document.getElementById("warning3").style.display="none";
            $("#submit").attr("disabled",true);
        }
    });

    $('#password').bind('input oninput', function(event) {
        var pwd = $(this).val();
        var rpwd =$("#username").val();
        if(rpwd==""){
            document.getElementById("warning3").style.display="block";
            document.getElementById("warning2").style.display="none";
            document.getElementById("warning").style.display="none";
            document.getElementById("warning1").style.display="none";
            $("#submit").attr("disabled",true);
        } else{
            if(pwd==rpwd){
                document.getElementById("warning").style.display="block";
                document.getElementById("warning1").style.display="none";
                document.getElementById("warning2").style.display="none";
                document.getElementById("warning3").style.display="none";
                $("#submit").attr("disabled",false);
            }else{
                document.getElementById("warning1").style.display="block";
                document.getElementById("warning2").style.display="none";
                document.getElementById("warning").style.display="none";
                document.getElementById("warning3").style.display="none";
                $("#submit").attr("disabled",true);
            }
        }
    });

});

function update() {
    //输入的新密码
    var first_password = $("#username").val();
    //输入的确认密码
    var new_password = $("#password").val();
    //输入的原密码
    var input_password = $("#pwd").val();
    $.ajax({
        type : "POST",
        url : "/resetPwd/reset",
        data : {
            "password": new_password,
            "oldPassword" : input_password
        },
        error : function(request) {
            layer.alert("系统错误，联系管理员");
        },
        success : function(data) {
            if (data.code == 0) {
                layer.alert("修改密码成功！系统将在3秒后退出！");
                $("#pwd").attr("value"," ");//将该input值清除，仅有这一步但界面上值依旧显示
                $("#pwd").val("");//将界面上的input值清空
                $("#username").attr("value"," ");
                $("#username").val("");
                $("#password").attr("value"," ");
                $("#password").val("");
                document.getElementById("warning").style.display="none";
                function go() {
                    top.window.location.href="/login";     //重定向方法
                }
                setTimeout(go,3000);
            }else if (data.code == -1){
                layer.alert("输入的当前密码错误！");
                $("#pwd").attr("value"," ");//将该input值清除，仅有这一步但界面上值依旧显示
                $("#pwd").val("");//将界面上的input值清空
                $("#username").attr("value"," ");
                $("#username").val("");
                $("#password").attr("value"," ");
                $("#password").val("");
                document.getElementById("warning").style.display="none";
            } else {
                layer.alert("修改密码失败！");
            }
        }
    });

}

